home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: Glen Parker <glenebob@halcyon.com>
- Newsgroups: comp.lang.c,comp.lang.c++
- Subject: Re: New printf - like function, is it possible?
- Date: Tue, 02 Apr 1996 12:43:56 -0800
- Organization: Computer Systems Contracting
- Message-ID: <3161918C.72D4@halcyon.com>
- References: <4eqb3a$2r5@pan.otol.fi> <311240d2.593861@news.demon.co.uk>
- NNTP-Posting-Host: blv-pm2-ip18.halcyon.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (WinNT; I)
-
- > >Do you C-gurus have any solutions to this problem?
- > >
- > >I am programming under MS Windows 3.1 (this is NOT an OS spesific
- > >question!)and I would like to create
- > >some kind of modified print function, which syntax should be something
- > >like normal printf; because in Windows printf can't be used, everything
- > >printed to screen has to come through message boxes etc, and before
- > >displaying anything, the string to displayed has to be formed. I have
- > >created my own printing function, which wants parameters string and winID
- > >to know where to put that string, something like this:
- > >
- > >sprintf(printStr, "some sfuff, variables %d, %f, etc", var1, var2);
- > >PrintToWin(DEBUG_WIN_1, printStr);
- > >
- > >However, I want to write a "better" printing function, where string to be
- > >formed doesn't need to be formatted first, it can be displayed and formed
- > >at the same time in the same syntax like printf, something like these:
- > >
- > >PrintToWin(DEBUG_WIN_1, "some stuff etc, %d, %f, %s, var1, var2, str1);
- > >PrintToWin(OTHER_WIN, "anything that printf accepts");
- > >
- > >So I want to print almost anything with this function without having
- > >to write many functions to different argument types/number of arguments.
- > >This should be possible in C++ (is it?), but is it possible in C ?
- > >Can this be achieved by using void pointers and macros etc., or should
- > >I give up and don't waste my time on this?
- > >
- > >Any help would be greatly appreciated. Thanks.
-
-
- Try this code:
- ---------------------------------------------
- char* Text;
-
- Text = (char*)malloc(6); //get memory
- sprintf(Text, "Hello"); //and stuff it
- //with formatted text
-
- MessageBox(GetFocus(), Text, "Debug", 0);
- free((void*)Text); //and free it again
- ---------------------------------------------
- or
- ---------------------------------------------
- char* Text;
-
- Text = (char*)malloc(6); //get memory
- sprintf(Text, "Hello"); //and stuff it
- //with formatted text
-
- TextOut(SomeDC, 0, 0, Text, strlen(Text));
- free((void*)Text); //and free it again
- ---------------------------------------------
-
-
- here is the documentation I got from my Borland C++ help file:
-
- ---------------------------------------------
- sprintf
-
- Syntax
-
- #include <stdio.h>
- int sprintf(char *buffer, const char *format[, argument, ...]);
-
- Description
-
- Writes formatted output to a string.
-
- Note: For details on format specifiers, see printf.
-
- sprintf accepts a series of arguments, applies to each a format specifier contained in the
- format string pointed to by format, and outputs the formatted data to a string.
- sprintf applies the first format specifier to the first argument, the second to the second,
- and so on. There must be the same number of format specifiers as arguments.
-
- Return Value
-
- On success, sprintf returns the number of bytes output. The return value does not include
- the terminating null byte in the count.
- On error, sprintf returns EOF.
- --------------------------------------------------
-
- Good Luck
-
- Glen Parker
-
-
-